from vec import *
import tkinter as tk
import _thread
import pgzrun

WIDTH = 800
HEIGHT = 440

BACK_COLOR = (0, 0, 0)

step = 10
fullLength = 500

rcH = 240
rcW = fullLength // step

pspd = rcW / fullLength
rcStep = rcW


SRC_COLOR = [158, 255, 62]
DEST_COLOR = [255, 111, 183]

class MyRect:
    def __init__(self, x, y, clr):
        self.pos = Vec2(x, y)
        self.clr = clr

    def Draw(self):
        rc = Rect((self.pos.x, self.pos.y), (rcW, rcH))
        screen.draw.filled_rect(rc, self.clr)

startX = WIDTH / 2 - fullLength / 2 - rcW

curX = startX
curY = 100

rc_list = []
p = 0

enable_lerp = True

def lerp_color(src_color, dest_color):
    global curX, p, rc_list, enable_lerp

    if p > 1.0:
        p = 1.0
        enable_lerp = False
        return

    cur_color = [0, 0, 0]

    for i in range(0, 3):
        cur_color[i] = src_color[i] * (1 - p) + dest_color[i] * p

    rc_list.append(MyRect(curX, curY, tuple(cur_color)))
    curX += rcStep
    p += pspd

def update():
    if enable_lerp == False:
        return

    lerp_color(SRC_COLOR, DEST_COLOR)

def draw():
    screen.clear()
    screen.fill(BACK_COLOR)
    for rc in rc_list:
        rc.Draw()

def tktk():
    global SRC_COLOR, DEST_COLOR
    mainWindow = tk.Tk()
    mainWindow.title("Color!")   
    mainWindow.geometry('240x160') 
    frame1 = tk.Frame(mainWindow)
    frame1.pack()
    fromLabel = tk.Label(frame1, text = "From:")
    fromLabel.pack(anchor = 'nw')

    fromRedLabel = tk.Label(frame1, text = "Red:")
    fromRedLabel.pack(side = tk.LEFT, anchor = 'nw')
    fromRedText = tk.Text(frame1, width = 5, height = 1)
    fromRedText.pack(side=tk.LEFT,anchor='nw')
    fromRedText.insert(1.0, '100')

    fromGreenLabel = tk.Label(frame1, text = "Green:")
    fromGreenLabel.pack(side = tk.LEFT, anchor = 'nw')
    fromGreenText = tk.Text(frame1, width = 5, height = 1)
    fromGreenText.pack(side = tk.LEFT, anchor = 'nw')
    fromGreenText.insert(1.0, '100')

    fromBlueLabel = tk.Label(frame1, text = "Blue:")
    fromBlueLabel.pack(side = tk.LEFT, anchor = 'nw')
    fromBlueText = tk.Text(frame1, width = 5, height = 1)
    fromBlueText.pack(side = tk.LEFT, anchor = 'nw')
    fromBlueText.insert(1.0, '100')

    frame2 = tk.Frame(mainWindow)
    frame2.pack()
    toLabel = tk.Label(frame2, text = "To:")
    toLabel.pack(anchor = 'nw')

    toRedLabel = tk.Label(frame2, text = "Red:")
    toRedLabel.pack(side = tk.LEFT, anchor = 'nw')
    toRedText = tk.Text(frame2, width = 5, height = 1)
    toRedText.pack(side = tk.LEFT, anchor = 'nw')
    toRedText.insert(1.0, '200')

    toGreenLabel = tk.Label(frame2, text = "Green:")
    toGreenLabel.pack(side = tk.LEFT, anchor = 'nw')
    toGreenText = tk.Text(frame2, width = 5, height = 1)
    toGreenText.pack(side = tk.LEFT, anchor = 'nw')
    toGreenText.insert(1.0, '200')

    toBlueLabel = tk.Label(frame2, text = "Blue:")
    toBlueLabel.pack(side = tk.LEFT, anchor = 'nw')
    toBlueText = tk.Text(frame2, width = 5, height = 1)
    toBlueText.pack(side = tk.LEFT, anchor = 'nw')
    toBlueText.insert(1.0, '200')

    frame3 = tk.Frame(mainWindow)
    frame3.pack()

    stepLabel = tk.Label(frame3, text = "Step:")
    stepLabel.pack(side = tk.LEFT, anchor = 'nw')
    stepText = tk.Text(frame3, width = 5, height = 1)
    stepText.pack(side = tk.LEFT, anchor = 'nw')
    stepText.insert(1.0, '10')

    frame4 = tk.Frame(mainWindow)
    frame4.pack()

    def onConfirm():
        global SRC_COLOR, DEST_COLOR, p, enable_lerp, rc_list, curX, step, rcW, pspd, rcStep, startX
        SRC_COLOR = [int(fromRedText.get(1.0, 'end')), int(fromGreenText.get(1.0, 'end')), int(fromBlueText.get(1.0, 'end'))]
        DEST_COLOR = [int(toRedText.get(1.0, 'end')), int(toGreenText.get(1.0, 'end')), int(toBlueText.get(1.0, 'end'))]
        step = int(stepText.get(1.0, 'end'))
        if step < 10:
            step = 10
            stepText.delete(1.0, 'end')
            stepText.insert(1.0, '10')
        elif step > 100:
            step = 100
            stepText.delete(1.0, 'end')
            stepText.insert(1.0, '100')
        rcW = fullLength // step

        pspd = rcW / fullLength
        rcStep = rcW

        p = 0        
        rc_list = []
        startX = WIDTH / 2 - fullLength / 2 - rcW
        curX = startX
        enable_lerp = True

    confirmBtn = tk.Button(frame4, text = "OK", width = 10, command = onConfirm)
    confirmBtn.pack()

    mainWindow.mainloop()

_thread.start_new_thread(tktk, ())

pgzrun.go()